//
//  HP_LoanTableViewCell.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/31.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "HP_LoanTableViewCell.h"

@implementation HP_LoanTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = ListMaoBackgroundColor;
        [self initialize];
    }
    return self;
}

-(void)updateUIWithModel:(LoanModel *)model{
    self.model = model;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.productImg] placeholderImage:[UIImage imageNamed:@""]];
    self.titleLabel.text = model.productName;
    int loanMin_int = [model.loanMin intValue];
    int loanMax_int = [model.loanMax intValue];
    CGFloat loanMin_float = [model.loanMin floatValue];
    CGFloat loanMax_float = [model.loanMax floatValue];
    NSString *loanMin_str;
    NSString *loanMax_str;
    
    
    if ([model.loanMin floatValue] >= 10000.0) {
        if (loanMin_int/10000 == loanMin_float/10000.f) {
            loanMin_str = [NSString stringWithFormat:@"%d万",loanMin_int/10000];
        }else{
            loanMin_str = [NSString stringWithFormat:@"%.f",loanMin_float/10000];
        }
    }else{
        loanMin_str = model.loanMin;
    }
    
    
    
    if ([model.loanMax floatValue] > 10000.0) {
        if (loanMax_int/10000 == loanMax_float/10000.f) {
            loanMax_str = [NSString stringWithFormat:@"%d万",loanMax_int/10000];
        }else{
            loanMax_str = [NSString stringWithFormat:@"%.f",loanMax_float/10000];
        }
    }else{
        loanMax_str = model.loanMax;
    }
    self.loanLimitLabel.text = [NSString stringWithFormat:@"%@-%@",loanMin_str,loanMax_str];
    
    
    CGFloat rate = [model.minRate floatValue];
    self.monthlyRateLabel.text = [NSString stringWithFormat:@"%.3f%@",rate,@"%"];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.iconImageView);
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(5);
        make.height.mas_offset(@15);
        make.width.mas_offset(@([HFQCommonTools calculateWidth:model.productName fontSize:15]));
    }];
    
    
    [self creatLabel:model.labelList.count view:self.configurationView array:model.labelList];
    
}

-(void)initialize{
    UIView *whiteView = [[UIView alloc]init];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.masksToBounds = YES;
    whiteView.layer.cornerRadius = 4;
    [self addSubview:whiteView];
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.mas_left).offset(12);
        make.right.mas_equalTo(self.mas_right).offset(-12);
        
    }];
    
    _iconImageView = [[UIImageView alloc]init];
    _iconImageView.image = [UIImage imageNamed:@"loan_Bitmap"];
    _iconImageView.layer.masksToBounds = YES;
    _iconImageView.layer.cornerRadius = 4;
    [self addSubview:_iconImageView];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteView.mas_left).offset(15);
        make.top.mas_equalTo(whiteView.mas_top).offset(18);
        make.width.height.mas_offset(@18);
    }];
    
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.text = @"借钱白条";
    _titleLabel.textColor = HexColor(0x383838);
    _titleLabel.font = UIFontBoldCustom(14);
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.iconImageView);
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(5);
        make.height.mas_offset(@15);
        make.width.mas_offset(@60);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = HexColor(0xE6E6E6);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(whiteView.mas_left).offset(15);
        make.right.mas_equalTo(whiteView.mas_right).offset(-15);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(11);
        make.height.mas_offset(@0.5);
    }];
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.text = @"可贷额度(元)";
    label1.textColor = ThemeTintFontColor;
    label1.font = [UIFont systemFontOfSize:12];
    [self addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(line);
        make.top.mas_equalTo(line.mas_bottom).offset(14);
        make.width.mas_offset(@80);
        make.height.mas_offset(@15);
    }];
    
    _loanLimitLabel = [[UILabel alloc]init];
    _loanLimitLabel.text = @"5000-10万";
    _loanLimitLabel.textColor = ThemeMaoBlackColor;
    _loanLimitLabel.font = UIFontBoldCustom(17);
    [self addSubview:_loanLimitLabel];
    [_loanLimitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label1);
        make.top.mas_equalTo(label1.mas_bottom).offset(6);
        make.width.mas_offset(@150);
    }];
    
    UILabel *label2 = [[UILabel alloc]init];
    label2.text = @"月利率";
    label2.textColor = ThemeTintFontColor;
    label2.font = [UIFont systemFontOfSize:12];
    [self addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(whiteView.mas_centerX);
        make.top.mas_equalTo(line.mas_bottom).offset(14);
        make.width.mas_offset(@60);
        make.height.mas_offset(@15);
    }];
    
    _monthlyRateLabel = [[UILabel alloc]init];
    _monthlyRateLabel.textColor = ThemeMaoBlackColor;
    _monthlyRateLabel.font = UIFontBoldCustom(17);
    _monthlyRateLabel.text = @"0.006%";
    [self addSubview:_monthlyRateLabel];
    [_monthlyRateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label2);
        make.centerY.mas_equalTo(self.loanLimitLabel);
        
    }];
    
    _applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_applyBtn setTitle:@"立即申请" forState:UIControlStateNormal];
    [_applyBtn setTitleColor:HexColor(0xFFFFFF) forState:UIControlStateNormal];
//    [_applyBtn setBackgroundColor:HexColor(0xBEA471)];
    _applyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _applyBtn.layer.masksToBounds = YES;
    _applyBtn.layer.cornerRadius = 15;
    _applyBtn.frame = CGRectMake(ScreenWidthMao-30-73, 100-28, 73, 28);
    [_applyBtn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_applyBtn];
    CAGradientLayer *caLayer = [CAGradientLayer layer];
    caLayer.frame = self.applyBtn.bounds;
    caLayer.startPoint = CGPointMake(0, 0);
    caLayer.endPoint = CGPointMake(1, 0);
    //设置颜色数组
    caLayer.colors = @[(__bridge id)HexColor(0xE4D1AD).CGColor,
                       (__bridge id)HexColor(0xBEA471).CGColor];
    
    //设置颜色分割点（范围：0-1）
    caLayer.locations = @[@(0.0f), @(1.0f)];
    [self.applyBtn.layer addSublayer:caLayer];
    
    UILabel *tiLabel = [[UILabel alloc]init];
    tiLabel.frame = self.applyBtn.frame;
    tiLabel.text = @"立即申请";
    tiLabel.font = [UIFont systemFontOfSize:12];
    tiLabel.textAlignment = NSTextAlignmentCenter;
    tiLabel.textColor = HexColor(0xFFFFFF);
    [self addSubview:tiLabel];
    
    _configurationView = [[UIView alloc]init];
    _configurationView.backgroundColor = [UIColor whiteColor];
    [self creatLabel:2 view:self.configurationView array:@[@"12小时放贷",@"期限12个月"]];
    [self addSubview:_configurationView];
    [_configurationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(5);
        make.right.mas_equalTo(self.applyBtn);
        make.centerY.mas_equalTo(self.titleLabel);
        make.height.mas_offset(@30);
    }];
    
    
    
    
    
    
    
    
    
    
    
}

-(void)tapAction{
    if ([self.delegate respondsToSelector:@selector(pushH5Action:)]) {
        [self.delegate pushH5Action:self.model];
    }
}

-(void)creatLabel:(NSInteger)count view:(UIView *)view array:(NSArray *)array{
    
    for (UIView *temView in view.subviews) {
        [temView removeFromSuperview];
    }
    
    CGFloat currentY = 0;
    CGFloat currentX = 0;
    
    
    CGFloat forwardHeight = 0;
    //间距
    CGFloat labelSpace = 3;
    //字体大小
    CGFloat fontFloat = 10;
    
    for (int i=0; i<count; i++) {
        NSString *title = array[i];
        UILabel *label = [[UILabel alloc]init];
        label.text = title;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = HexColor(0x999999);
        label.layer.masksToBounds = YES;
        label.layer.borderColor = HexColor(0xCCCCCC).CGColor;
        label.layer.borderWidth = .5f;
        label.layer.cornerRadius = 1;
        label.font = [UIFont systemFontOfSize:fontFloat];
        [view addSubview:label];
        
        CGSize size = [self boundingRectWithSize:array[array.count -count + i] Font:[UIFont systemFontOfSize:fontFloat] Size:CGSizeMake(ScreenWidthMao - 40, 40)];
        if (currentX + size.width >ScreenWidthMao - 10) {
            currentX = labelSpace;
            currentY = currentY + forwardHeight + 10;
        }
        label.frame = CGRectMake(currentX, 7, size.width, 16);
        [view addSubview:label];
        forwardHeight = 25;
        currentX = currentX + size.width + 8;
        
    }
}

- (CGSize) boundingRectWithSize:(NSString*)string Font:(UIFont*) font Size:(CGSize) size{
    CGSize _size;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    NSDictionary *attribute = @{NSFontAttributeName: font};
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    NSStringDrawingUsesLineFragmentOrigin |
    NSStringDrawingUsesFontLeading;
    _size = [string boundingRectWithSize:size options: options attributes:attribute context:nil].size;
#else
    _size = [string sizeWithFont:font constrainedToSize:size];
#endif
    _size.height += 10;
    _size.width += 10;
    return _size;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
